<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <base href="${APP_PATH}/">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>教学管理系统</title>
    <link rel="icon" href="public/favicon.ico" type="image/ico">
    <%@include file="/commons.jsp" %>
    <link rel="stylesheet" href="public/js/jconfirm/jquery-confirm.min.css">
    <link rel="stylesheet" href="public/css/style.min.css">
</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <%@ include file="/menu.jsp" %>
        <!--End 左侧导航-->
        <!--头部信息-->
        <%@ include file="/head.jsp" %>
        <!--End 头部信息-->
        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <form class="pull-right search-bar" id="search">
                                    <div class="input-group">
                                        <input type="text" class="form-control" id="condition" name="condition" placeholder="请输入角色名称">
                                        <div class="input-group-btn">
                                            <button class="btn btn-default dropdown-toggle" id="searchBtn" type="button">搜索</button>
                                        </div>
                                    </div>
                                </form>
                                <div class="toolbar-btn-action">
                                    <a class="btn btn-primary m-r-5" href="/manage/role/add"><i class="mdi mdi-plus"></i> 新增</a>
                                    <a class="btn btn-danger" onclick="delAll()"><i class="mdi mdi-window-close"></i> 删除</a>
                                </div>
                            </div>
                            <div class="card-body">
                                <form id="dataForm">
                                    <table class="table  table-bordered">
                                        <thead>
                                        <tr>
                                            <th width="100"><label class="lyear-checkbox checkbox-primary">
                                                <input type="checkbox" id="check-all"><span></span>
                                            </label></th>
                                            <th width="120">序号</th>
                                            <th>名称</th>
                                            <th width="200">操作</th>
                                        </tr>
                                        </thead>
                                        <tbody id="data">
                                        </tbody>
                                    </table>
                                    <div align="center">
                                        <ul class="pagination" id="pages"></ul>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>
<script type="text/javascript">
    $(function () {
        layer.msg("加载中", {time: 200, icon: 16, shift: 5}, function () {
            goPage(1);
            $("#delete").click(function () {
                $.ajax({
                    url: "manage/role/delete",
                    type: "post",
                    data: $("#roleForm").serialize(),
                    dataType: "json",
                    success: function (obj) {
                        if (obj.result) {
                            layer.msg("删除成功", {time: 1000, icon: 6, shift: 6}, function () {
                                goPage(1);
                            })
                        } else {
                            layer.msg("删除失败", {time: 1000, icon: 5, shift: 6}, function () {
                            })
                        }
                    }
                })
            })
        });//layer
    });//入口

    function goPage(pageNum) {
        $.ajax({
            url: "manage/role/indexDo?pageNum=" + pageNum,
            type: "post",
            data: $("#search").serialize(),
            dataType: "json",
            success: function (obj) {
                let listHtml = template("listTpl", {
                    roles: obj.roleList
                });
                $("#data").html(listHtml);
                let prePage = obj.pageInfo.pageNum == 1 ? 1 : obj.pageInfo.pageNum - 1;
                let nextPage = obj.pageInfo.pageNum == obj.pageInfo.pages ? obj.pageInfo.pages : obj.pageInfo.pageNum + 1;
                var pageMessage = "";
                pageMessage += '<li class="' + (obj.pageInfo.pageNum == 1 ? "disabled" : "") + '"><a onclick="goPage(' + prePage + ')">上一页</a></li>';
                for (var i = 1; i <= obj.pageInfo.pages; i++) {
                    pageMessage += '<li class="' + (obj.pageInfo.pageNum == i ? "active" : "") + '"><a onclick="goPage(' + i + ')">' + i + ' <span class="sr-only">(current)</span></a></li>';
                }
                pageMessage += '<li class="' + (obj.pageInfo.pageNum == obj.pageInfo.pages ? "disabled" : "") + '"><a onclick="goPage(' + nextPage + ')">下一页</a></li>';
                $("#pages").html(pageMessage);
                $("#searchBtn").unbind();
                $("#searchBtn").click(function () {
                    goPage(obj.pageInfo.pageNum);
                })
            }
        })
    }

    function del(rid) {
        if (confirm("确认删除吗？")) {
            $.ajax({
                url: "manage/role/delete?rid=" + rid,
                type: "post",
                dataType: "json",
                success: function (obj) {
                    if (obj.result) {
                        layer.msg("删除成功", {time: 1000, icon: 6, shift: 6}, function () {
                            goPage(1);
                        })
                    } else {
                        layer.msg("删除失败", {time: 1000, icon: 5, shift: 6}, function () {
                        })
                    }
                }
            })
        }
    }

    function delAll() {
        if (confirm("确认删除吗？")) {
            $.ajax({
                url: "manage/role/delAll",
                data: $("#dataForm").serialize(),
                type: "post",
                success: function (obj) {
                    if (obj.result) {
                        layer.msg("删除角色成功", {time: 1000, icon: 6, shift: 6}, function () {
                            location.href = "manage/user/index"
                        })
                    } else {
                        layer.msg("删除角色失败", {time: 1000, icon: 5, shift: 6}, function () {
                        })
                    }
                }
            })
        }
    }

    function assignPermission(rid) {
        location.href = "manage/permission/assignPermission?rid=" + rid;
    }

    function update(rid) {
        location.href = "manage/role/update?rid=" + rid;
    }
</script>
</body>
</html>
<script id="listTpl" type="text/html">
    {{each roles role index}}
    <tr>
        <td>
            <label class="lyear-checkbox">
                <input type="checkbox" name="rids" value="{{role.rid}}"><span></span>
            </label>
        </td>
        <td>{{index+1}}</td>
        <td>{{role.rname}}</td>
        <td>
            <div class="btn-group">
                <a class="btn btn-xs btn-default" onclick="assignPermission('{{role.rid}}')" title="修改权限" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                <a class="btn btn-xs btn-default" onclick="update('{{role.rid}}')" title="修改角色" data-toggle="tooltip"><i class="mdi mdi-account"></i></a>
                <a class="btn btn-xs btn-default" onclick="del('{{role.rid}}')" title="删除用户" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>
            </div>
        </td>
    </tr>
    {{/each}}
</script>
